// orderDetail.js
const regeneratorRuntime = require('../../lib/runtime');
const Api = require('../../utils/api.js');
const wx = require('../../lib/wx');
const wxapp = require('../../lib/wxapp');
const Util = require('../../utils/util.js');

// 获取应用实例
const app = getApp();
Page({
  data: {
    isImported: false,
    orderInfo: {
      orderId: '15174987643330112621491',
      createDate: '2018年02月01日 23:26:04',
      color: '白色',
      size: '45',
      productNum: '1',
      price: '53.00',
      logo: 'http://www.haba.com.cn/images/logo3.png',
      shop: 'HABA',
      img: 'http://www.haba.com.cn/UploadFiles/ProductImg/110df2c5-447f-4d46-863e-75d059b47e48.png',
      express: '直邮中国',
      expressIcon: '../../images/icon_flag_china.png',
      productInfo: 'HABA 日本无添加主义丝滑泡沫洁面皂 80g',
      freight: '8.00',
      total: '61.00'
    },
    userInfo: {
      name: '隔壁老王',
      address: '北京-北京市-东城区 隔壁路老王街',
      phone: '13858588858',
    },
    modalHide: true,
  },

  showTips: wxapp.handler(function* showTips(next){
    let modalHide = this.data.modalHide;
    if(modalHide){
      modalHide = false;
    } else{
      modalHide = true;
    }
    this.setData({
      modalHide,
    })

    yield next;
  }),

  hideTips: wxapp.handler(function* hideTips(next){
    this.setData({
      modalHide: true,
    });

    yield next;
  }),

  goToPay:wxapp.handler(function* goToPay(next){
    var order_id = this.data.orderInfo.orderId;
    var wxPay = Api.wxPay();
    const authTokenKey = Util.authTokenKey();
    var token = wx.getStorageSync(authTokenKey);

    wx.request({
      url: wxPay,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data: {
        order_id: order_id,
        token:token
      },
      complete: function (res) {
        console.log(res);
        if(res.data.status != 0) {
          var order = res.data.data;
          if(order.nonceStr) {
            wx.requestPayment({
              timeStamp: order.timeStamp,
              nonceStr: order.nonceStr,
              package: order.package,
              signType: 'MD5',
              paySign: order.paySign,
              success: function (res) {
                wx.showToast({
                  title: "支付成功!",
                  duration: 2000,
                  complete: function () { 
                    setTimeout(function () {
                      wx.navigateTo({
                        url: '../me/me',
                      });
                    }, 3000);
                  }
                });
              
              },
              fail: function (res) {
                wx.showToast({
                  title: res,
                  image: '../../images/error.png',
                  duration: 3000
                })
              },
              complete:function(res){
                if (res.errMsg == 'requestPayment:ok'){
                  wx.showToast({
                    title: "支付成功!",
                    duration: 2000,
                    complete: function () {
                      setTimeout(function () {
                        wx.navigateTo({
                          url: '../me/me',
                        });
                      }, 3000);
                    }
                  });
                }
              }
            })
          }else{
            wx.showToast({
              title: '系统错误',
              image: '../../images/error.png',
              duration: 3000
            })
          }
        }else{
          wx.showToast({
            title: res.data.msg,
            image: '../../images/error.png',
            duration: 3000
          })
        }
      }
    });

    yield next;
  }),

  onShow: wxapp.handler(function* onShow(next) {
    
    yield next;
  }),

  onLoad: wxapp.handler(function* onLoad(next,e) {
    var orderId = e.orderId;
    var that = this;
    var getOrderDetail = Api.getOrderDetail();
    wx.request({
      url: getOrderDetail,
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      data: {//发送给后台的数据
        order_id: orderId
      },
      complete: function (res) {
          that.setData({
            orderInfo:res.data.data,
            userInfo: res.data.data.userInfo
          });
      }
    })

    yield next;
  }),

});
